package behavioral_pattern.mediator_pattern.concrete_colleague;

import behavioral_pattern.mediator_pattern.colleague.IDepartment;
import behavioral_pattern.mediator_pattern.mediator.IMediator;

/*
 * @Author xuhj
 * @Description 库存部门
 * @Date 12:17 下午 2022/1/9
 * @Param
 * @return
 **/
public class StockDepartment extends IDepartment {
    public StockDepartment(IMediator mediator) {
        super(mediator);
    }

    public void setProductNum(Integer productNum) {
        this.productNum = productNum;
    }

    @Override
    public void work(Integer num) {
        System.out.println("当前库存:"+this.productNum);
        // 补货成功标志
        boolean flag = false;
        if (this.productNum >= num) {
            System.out.println("取走存货"+num+"件");
            this.productNum -= num;
            flag = true;
        } else {
            System.out.println("存货不足, 无法补货. 需要采购");
            flag = false;
        }

        // 这里应该是异步通知仓库不足,需要采买
        if (productNum > 100) {
            System.out.println("仓库存库充足!");
        } else {
            communicat(COMMUNICATION_TYPE_PURCHASE);
        }

        if (flag) {
            communicat(COMMUNICATION_TYPE_ADD_PRODUCT_FINISH);
        } else {
            communicat(COMMUNICATION_TYPE_PURCHASEING);
        }
    }

    @Override
    public void communicat(String communicationType) {
        if (communicationType.equals(COMMUNICATION_TYPE_PURCHASE)) {
            //System.out.println("仓库库存不足, 需要采购");
            mediator.operate(COMMUNICATION_TYPE_PURCHASE, 0);
        } else if (communicationType.equals(COMMUNICATION_TYPE_ADD_PRODUCT_FINISH))  {
            // System.out.println("仓库库存补货完成");
            mediator.operate(COMMUNICATION_TYPE_ADD_PRODUCT_FINISH, 0);
        } else if(communicationType.equals(COMMUNICATION_TYPE_PURCHASEING)) {
            // 库存不足,采买中....
            mediator.operate(COMMUNICATION_TYPE_ADD_PRODUCT_FINISH, 0);
        }
    }
}
